package Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import entity.StudentInfo;

public class StudentInfoDao extends BaseDao{
	public Connection conn;
	public PreparedStatement pstmt;
	public ResultSet  rs;

	public List<StudentInfo> getList(){
		
		List<StudentInfo> list=new ArrayList<StudentInfo>();
		
		try {

			conn=super.getConnection();

			String sql="select * from studentInfo ORDER BY sid DESC";

			pstmt=conn.prepareStatement(sql);

			rs=pstmt.executeQuery();

			while (rs.next()) {
				StudentInfo stu=new StudentInfo();	
				stu.setSid(rs.getInt("sid"));
				stu.setSname(rs.getString("sname"));
				stu.setSaddress(rs.getString("saddress"));
				stu.setSage(rs.getInt("sage"));
				stu.setSemail(rs.getString("semail"));
				stu.setSgender(rs.getString("sgender"));
				list.add(stu);
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			super.closeAll(conn, pstmt, rs);
		}

		return list;
	}
	public StudentInfo getStu(String sname){
		
		StudentInfo stu = null;
		
		try {

			conn=super.getConnection();

			String sql="select * from studentInfo where sname=? ORDER BY sid DESC";

			pstmt=conn.prepareStatement(sql);

			pstmt.setObject(1, sname);
			
			rs=pstmt.executeQuery();

			while (rs.next()) {
			    stu=new StudentInfo();	
				stu.setSid(rs.getInt("sid"));
				stu.setSname(rs.getString("sname"));
				stu.setSaddress(rs.getString("saddress"));
				stu.setSage(rs.getInt("sage"));
				stu.setSemail(rs.getString("semail"));
				stu.setSgender(rs.getString("sgender"));
				
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			super.closeAll(conn, pstmt, rs);
		}

		return stu;
	}
	public int deteleStu(int sid){
		int reslut=0;	
		try {

			conn=super.getConnection();

			String sql="DELETE from studentInfo WHERE sid=?";

			pstmt=conn.prepareStatement(sql);

			pstmt.setInt(1, sid);
			
			reslut=pstmt.executeUpdate();

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			super.closeAll(conn, pstmt, rs);
		}

		return reslut;
	}
	public int updateStu(StudentInfo stu){
		int reustl=0;
		try {
			conn=super.getConnection();
			
			String sql="UPDATE  StudentInfo set  sname=?,sgender=?, sage=?,saddress=?,semail=? WHERE sid=? ";
			
			pstmt=conn.prepareStatement(sql);
			
			pstmt.setInt(6, stu.getSid());
			pstmt.setString(1, stu.getSname());
			pstmt.setString(2,stu.getSgender());
			pstmt.setInt(3, stu.getSage());
			pstmt.setString(4, stu.getSaddress());
			pstmt.setString(5, stu.getSemail());
			
			reustl=pstmt.executeUpdate();
			
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			super.closeAll(conn, pstmt, rs);
		}

		return reustl;		
	}
}
